package com.reone.jsbridgedemo.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.widget.EditText;
import android.widget.TextView;

import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.reone.jsbridgedemo.activity.base.BaseActivity;
import com.reone.jsbridgedemo.webview.JSBridgeWebView;
import com.reone.jsbridgedemo.R;
import com.reone.jsbridgedemo.utils.Utils;
import com.reone.talklibrary.TalkApp;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wangxingsheng on 2018/7/13.
 */
public class MainActivity extends BaseActivity {
    @BindView(R.id.js_bridge_webView)
    JSBridgeWebView jsBridgeWebView;
    @BindView(R.id.edit_jump)
    EditText editJump;
    @BindView(R.id.text_show)
    TextView textShow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        jsBridgeWebView.addJavascriptInterface(this,"MainActivity");
        initJsBridge();
    }

    private void initJsBridge() {
        jsBridgeWebView.getBridge().setDefaultHandler(new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                TalkApp.talk(data);
            }
        });
        jsBridgeWebView.getBridge().registerHandler("showJsBridge", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                TalkApp.talk("handler:" + data);
                function.onCallBack("CallBackFunction");
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        jsBridgeWebView.loadUrl(Utils.getAssetHtml("jsdemo.html"));
    }

    @JavascriptInterface
    public String getText(){
        return Utils.getTextFromEdit(editJump);
    }

    @JavascriptInterface
    public String getJumpUrl(){
        return Utils.getTextFromEdit(editJump);
    }

    @OnClick({R.id.invoke_edit_param, R.id.invoke_no_param, R.id.reset, R.id.refresh, R.id.jump_second,R.id.invoke_by_js_bridge})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.invoke_edit_param:
                jsBridgeWebView.loadUrl(
                        Utils.getJsMethodInvokeName(
                                "message2",
                                Utils.getTextFromEdit(editJump)
                        )
                );
                break;
            case R.id.invoke_no_param:
                jsBridgeWebView.loadUrl(Utils.getJsMethodInvokeName("message"));
                break;
            case R.id.reset:
                jsBridgeWebView.loadUrl(Utils.getAssetHtml("jsdemo.html"));
                break;
            case R.id.refresh:
                jsBridgeWebView.reload();
                break;
            case R.id.invoke_by_js_bridge:
                jsBridgeWebView.getBridge().callHandler("jsFunction", "android tell h5 show js bridge", new CallBackFunction() {
                    @Override
                    public void onCallBack(String data) {
                        TalkApp.talk("onCallBack: "+data);
                    }
                });
                break;
            case R.id.jump_second:
                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Utils.getTextFromEdit(editJump)));
                    startActivity(intent);
                }catch (Exception e){
                    TalkApp.talk(e.getMessage());
                }
                break;
        }
    }
}
